package Leetcode_Second_Brush;

public class Solution002 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode node = new ListNode();
        ListNode result = node;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int value1 = l1 == null ? 0 : l1.val;
            int value2 = l2 == null ? 0 : l2.val;
            ListNode next = new ListNode((carry + value1 + value2) % 10);
            node.next = next;
            node = next;
            carry = (carry + value1 + value2) / 10;
            l1 = l1 == null ? null : l1.next;
            l2 = l2 == null ? null : l2.next;
        }
        if (carry != 0) {
            node.next = new ListNode(carry);
        }
        return result.next;
    }
}
